package org.fs.dao;

// Generated 2009-02-21 16:41:17 by Hibernate Tools 3.2.4.CR1

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fs.entities.Flight;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

/**
 * Home object for domain model class Flight.
 * @see org.fs.entities.Flight
 * @author Hibernate Tools
 */
public class FlightHome {

	private static final Log log = LogFactory.getLog(FlightHome.class);

	private  SessionFactory sessionFactory;

//	protected SessionFactory getSessionFactory() {
//		try {
//			return (SessionFactory) new InitialContext()
//					.lookup("SessionFactory");
//		} catch (Exception e) {
//			log.error("Could not locate SessionFactory in JNDI", e);
//			throw new IllegalStateException(
//					"Could not locate SessionFactory in JNDI");
//		}
//	}

	public void persist(Flight transientInstance) {
		log.debug("persisting Flight instance");
		try {
			Session s = sessionFactory.getCurrentSession();
			Transaction trx = s.beginTransaction();
			s.persist(transientInstance);
			trx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Flight instance) {
		log.debug("attaching dirty Flight instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Flight instance) {
		log.debug("attaching clean Flight instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Flight persistentInstance) {
		log.debug("deleting Flight instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Flight merge(Flight detachedInstance) {
		log.debug("merging Flight instance");
		try {
			Flight result = (Flight) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Flight findById(java.math.BigDecimal id) {
		log.debug("getting Flight instance with id: " + id);
		try {
			Flight instance = (Flight) sessionFactory.getCurrentSession().get(
					"org.fs.entities.Flight", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Flight> findByExample(Flight instance) {
		log.debug("finding Flight instance by example");
		try {
			List<Flight> results = (List<Flight>) sessionFactory
					.getCurrentSession().createCriteria(
							"org.fs.entities.Flight").add(create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
